<template>
	<u-upload 
		ref="Upload" 
		:form-data="formData" 
		:header="header" 
		:name="name" 
		:beforeUpload="beforeUpload" 
		:action="action" 
		:auto-upload="true" 
		:width="width" 
		:height="height" 
		:max-count="maxCount"
		:file-list="fileList"
		:max-size='config.max_image_size'
		:show-progress="showProgress"
		@on-success="success"
		@on-remove="remove">
	</u-upload>
</template>

<script>
	const app = getApp()
	import request from '../../api/request.js'
	import login from '../../api/login.js'
	export default{
		data(){
			return{
				config:app.globalData.config,
				
				header:{
					"Content-Type":"application/x-www-form-urlencoded"
				},
				formData:{
					token:""
				}
			}
		},
		methods:{
			beforeUpload(index,list){
				return new Promise(async (resolve, reject) => {
					await login.checkLogin()
					this.formData.token = app.globalData.userInfo?app.globalData.userInfo.token:''
					resolve()
				})
			},
			success(e){
				if(e.code == 200){
					let data = e.data
					this.$emit("success",data)
				}else{
					this.$refs.Upload.remove(this.$refs.Upload.lists.length-1)
					uni.showToast({
						icon:'none',
						title:e.message
					})
				}
			},
			remove(e){
				this.$emit("remove",e)
			}
		},
		props:{
			file:{
				type:String,
			},
			action:{
				type:String,
				default:""
			},
			name:{
				type:String,
				default:""
			},
			width:{
				type:[String,Number],
				default:188,
			},
			height:{
				type:[String,Number],
				default:188,
			},
			maxCount:{
				type:[String,Number],
				default:99,
			},
			fileList:{
				type:Array,
				default:[]
			},
			showProgress:{
				type:Boolean,
				default:true
			}
		}
	}
</script>

<style>
</style>
